package com.xueyu.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.domain.goods.Details;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DetailsDao extends BaseMapper<Details> {


    @Select("SELECT od.*, d.*,drugs.drugnames,supplier.supplierName FROM details od  JOIN drug d ON od.prodid = d.DrugID JOIN drugs ON d.DrugF = drugs.drugid JOIN supplier ON d.supplier = supplier.supplierId  WHERE od.prodid IN (${orderNos})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "itemid", column = "itemid"),
            @Result(property = "prodid", column = "prodid"),
            @Result(property = "houseid", column = "houseid"),
            @Result(property = "quantity", column = "quantity"),
            @Result(property = "drug.prodcount", column = "quantity"),
            @Result(property = "purchase", column = "purchase"),
            @Result(property = "expiration", column = "expiration"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at"),

            @Result(property = "drug.DrugID", column = "DrugID"),
            @Result(property = "drug.DrugName", column ="DrugName"),
            @Result(property = "drug.DrugPic", column = "DrugPic"),
            @Result(property = "drug.DrugF", column = "drugnames"),
            @Result(property = "drug.style", column = "style"),
            @Result(property = "drug.supplier", column = "supplierName"),
            @Result(property = "drug.DrugP", column = "DrugP"),
            @Result(property = "drug.DrugM", column = "DrugM"),
            @Result(property = "drug.price", column = "price"),
            @Result(property = "drug.ctime", column = "ctime"),
            @Result(property = "drug.Gtime", column = "Gtime")
    })
    List<Details> selectOrderDetailWithDrugsByOrderNoList(@Param("orderNos") String orderNos);



    @Select("SELECT od.*, d.*,products.productName,supplier.supplierName FROM details od  JOIN product d ON od.itemid = d.prodId JOIN products ON d.style = products.productId JOIN supplier ON d.supplierId = supplier.supplierId  WHERE od.itemid IN (${orderNos})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "itemid", column = "itemid"),
            @Result(property = "prodid", column = "prodid"),
            @Result(property = "houseid", column = "houseid"),
            @Result(property = "quantity", column = "quantity"),
            @Result(property = "drug.prodcount", column = "quantity"),
            @Result(property = "purchase", column = "purchase"),
            @Result(property = "expiration", column = "expiration"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at"),
            @Result(property = "drug.DrugID", column = "prodid1"),
            @Result(property = "drug.DrugName", column = "prodName"),
            @Result(property = "drug.DrugPic", column = "brand"),
            @Result(property = "drug.DrugF", column = "productName"),
            @Result(property = "drug.style", column = "unitName"),
            @Result(property = "drug.supplier", column = "supplierName"),
            @Result(property = "drug.DrugM", column = "DrugM"),
            @Result(property = "drug.price", column = "price"),
            @Result(property = "drug.ctime", column = "ctime"),
            @Result(property = "drug.Gtime", column = "Gtime")
    })
    List<Details> selectOrderDetailWithDrugsByOrderNoList2(@Param("orderNos") String orderNos);



    @Select("<script>" +
            "SELECT DISTINCT od.prodid " +
            "FROM details  od  JOIN drug d ON od.prodid = d.DrugID " +
            "WHERE 1=1 "
            +
            "<if test='orderno != null and orderno != \"\"'>" + //查询药品id
            "AND d.supplier = #{orderno} " +
            "</if>"
            +
            "<if test='ord != null and ord != \"\"'>" +//分类
            "AND d.DrugF = #{ord} " +
            "</if>"
            +
            "<if test='name != null and name != \"\"'>" +//名字
            "AND d.DrugName = #{name} " +
            "</if>"
            +
            "</script>")
    List<String> getOrderNoList(Page page,@Param("orderno") String orderno,@Param("ord") String ord,@Param("name") String name);




    @Select("<script>" +
            "SELECT DISTINCT od.prodid " +
            "FROM details  od  JOIN drug d ON od.prodid = d.DrugID " +
            "WHERE 1=1 "
            +
            "</script>")
    List<String> getList();




    @Select("<script>" +
            "SELECT DISTINCT od.itemid " +
            "FROM details  od  JOIN product d ON od.itemid = d.prodId " +
            "WHERE 1=1 "
            +
            "<if test='orderno != null and orderno != \"\"'>" + //查询药品id
            "AND d.supplierId = #{orderno} " +
            "</if>"
            +
            "<if test='ord != null and ord != \"\"'>" +//分类
            "AND d.style = #{ord} " +
            "</if>"
            +
            "<if test='name != null and name != \"\"'>" +//名字
            "AND d.prodName = #{name} " +
            "</if>"
            +
            "</script>")
    List<String> getOrderNoList2(Page page,@Param("orderno") String orderno,@Param("ord") String ord,@Param("name") String name);


    @Select("<script>" +
            "SELECT DISTINCT od.itemid " +
            "FROM details  od  JOIN product d ON od.itemid = d.prodId " +
            "WHERE 1=1 "
            +
            "</script>")
    List<String> getList2();


    @Update("<script>" +
            "UPDATE details " +
            "<set>" +
            "quantity = CASE itemid " +
            "<foreach collection='prodids' item='itemid' index='index' separator=' '>" +
            "WHEN #{itemid} THEN #{prodcounts[${index}]} " +
            "</foreach>" +
            "END, " +
            "updated_at = #{currentTime} " +
            "</set>" +
            "WHERE itemid IN " +
            "<foreach collection='prodids' item='itemid' open='(' separator=',' close=')'>" +
            "#{itemid}" +
            "</foreach>" +
            "</script>")
    void updateProdCountsBatchs(@Param("prodids") List<Integer> prodids, @Param("prodcounts") List<Integer> prodcounts, @Param("currentTime") String currentTime);


    @Update("<script>" +
            "UPDATE details " +
            "<set>" +
            "quantity = CASE prodid " +
            "<foreach collection='prodids' item='prodid' index='index' separator=' '>" +
            "WHEN #{prodid} THEN #{prodcounts[${index}]} " +
            "</foreach>" +
            "END, " +
            "updated_at = #{currentTime} " +
            "</set>" +
            "WHERE prodid IN " +
            "<foreach collection='prodids' item='prodid' open='(' separator=',' close=')'>" +
            "#{prodid}" +
            "</foreach>" +
            "</script>")
    void updates(@Param("prodids") List<Integer> prodids, @Param("prodcounts") List<Integer> prodcounts, @Param("currentTime") String currentTime);




    @Update("<script>" +
            "UPDATE details " +
            "<set>" +
            "quantity = CASE itemid " +
            "<foreach collection='prodids' item='prodid' index='index' separator=' '>" +
            "WHEN #{prodid} THEN #{prodcounts[${index}]} " +
            "</foreach>" +
            "END, " +
            "updated_at = #{currentTime} " +
            "</set>" +
            "WHERE itemid IN " +
            "<foreach collection='prodids' item='prodid' open='(' separator=',' close=')'>" +
            "#{prodid}" +
            "</foreach>" +
            "</script>")
    void updates1(@Param("prodids") List<Integer> prodids, @Param("prodcounts") List<Integer> prodcounts, @Param("currentTime") String currentTime);





    @Update("<script>" +
            "UPDATE details " +
            "<set>" +
            "quantity = CASE prodid " +
            "<foreach collection='prodids' item='prodid' index='index' separator=' '>" +
            "WHEN #{prodid} THEN #{prodcounts[${index}]} " +
            "</foreach>" +
            "END, " +
            "updated_at = #{currentTime} " +
            "</set>" +
            "WHERE prodid IN " +
            "<foreach collection='prodids' item='prodid' open='(' separator=',' close=')'>" +
            "#{prodid}" +
            "</foreach>" +
            "</script>")
    void updateProdCountsBatch(@Param("prodids") List<Integer> prodids, @Param("prodcounts") List<Integer> prodcounts, @Param("currentTime") String currentTime);


    @Update("<script>" +
            "UPDATE details " +
            "<set>" +
            "quantity = #{prodcounts}," +
            "updated_at =#{currentTime2}"+
            "</set>" +
            "WHERE 1=1 " +
            "<if test='prodidss != null and prodidss != \"\"'>" +
            "AND prodid = #{prodidss} " +
            "</if>"
            +
            "</script>")
    int  updateprodcounts (@Param("prodidss")  int prodidss, @Param("prodcounts")  int prodcounts,@Param("currentTime2") String currentTime2);






    @Update("<script>" +
            "UPDATE details " +
            "<set>" +
            "quantity = #{prodcounts}," +
            "updated_at =#{currentTime2}"+
            "</set>" +
            "WHERE 1=1 " +
            "<if test='prodidss != null and prodidss != \"\"'>" +
            "AND itemid = #{prodidss} " +
            "</if>"
            +
            "</script>")
    int  update(@Param("prodidss")  int prodidss, @Param("prodcounts")  int prodcounts,@Param("currentTime2") String currentTime2);






}


